<?php 

require_once('xslt.php');
require_once('rdf2dot.php');


$src=$_GET['src'];

$format = 'xml';
if (isset($_GET['format']))
{
	$format=$_GET['format'];
}

$filename = $config['cache_dir'] . "/" . $src;


$cache_file = @fopen($filename, "r") or die("could't open file \"$filename\"");
$xml = @fread($cache_file, filesize ($filename));
fclose($cache_file);

$result = '';

switch ($format)
{
	case 'xml':
		$xslt_file = 'xsl/xmlverbatim.xsl';
		$result = XSLT_Buffer ($xml, $xslt_file);
		break;

	case 'rdf':
		$xslt_file = 'xsl/rdf2embeddedhtml.xsl';
		$result = XSLT_Buffer ($xml, $xslt_file);
		break;
		
	case 'raw':
		$result = "<pre>" . htmlspecialchars($xml) . "</pre>";
		break;
		
	case 'png':
	case 'svg':
		$result .= "<html>\n";
		$result .= "<head>\n";
		$result .= "<style type=\"text/css\">\n";
		$result .= "body {\n";
		$result .= "	font-family: Lucida Grande,Verdana,Arial,Helvetica, sans-serif;\n";
		$result .= "	font-size: 11px;\n";
		$result .= "};\n";
		$result .= "</style>\n";
		$result .= "</head>\n";
		$result .= "<body>\n";
		$dot_file_name = $filename;
		$dot_file_name = str_replace (".rdf", "", $dot_file_name);
		$dot_file_name .= ".dot";
/*		$image_file_name = $dot_file_name . ".png";
		
		if (!file_exists($dot_file_name) && !file_exists($image_file_name))
		{
			$dot = rdf2dot ($filename);
			$dot_file = @fopen($dot_file_name, "w+") or die("could't open file --\"$dot_file_name\"");
			@fwrite($dot_file, $dot);
			fclose($dot_file_name);

			$command = $config['dot'] . " -Tpng -o$image_file_name " .  $dot_file_name;
			system($command);
		}
		$dot_web = $config['cache_prefix'] . "/" . $src . ".dot.png";
		$result = "<a href=\"get.php?src=" . $src . "&format=dot\" target=\"_blank\">dot file</a><br/>";
		$result .= "<img src=\"$dot_web\" width=\"500\">";*/
		
		//$result .= "<i>$dot_file_name</i>";

		if (!file_exists($dot_file_name))
		{
			$dot = rdf2dot ($filename);
			$dot_file = @fopen($dot_file_name, "w+") or die("could't open file --\"$dot_file_name\"");
			@fwrite($dot_file, $dot);
			fclose($dot_file_name);
		}
		$dot_web = $src;
		$dot_web = str_replace(".rdf", "", $dot_web);
		$dot_web = $config['cache_prefix'] . "/" . $dot_web . ".dot." . $format;;
		$dot_image = $config['webdot'] . "/" .  $config['webroot'] . "/" . $dot_web;
		$result .= "View <a href=\"get.php?src=" . $src . "&format=dot\" target=\"_blank\">dot file</a><br/>\n";
		$result .= "View image <a href=\"$dot_image\" target=\"_blank\">in new window</a><br/>\n";

		if ($format == 'svg')
		{
			$result .= "<embed src=\"$dot_image\" type=\"image/svg+xml\"  width=\"500\" height=\"500\">";	
		}
		else
		{
			$result .= "<img src=\"$dot_image\" width=\"500\">\n";
		}
	
		$result .= "</body>\n";
		$result .= "</html>\n";
		
		break;


	case 'dot':
		header("Content-Type: text/plain");
		$result = rdf2dot ($filename);
		break;


		
	default:
		$result = $xml;
		break;


}




echo $result;

?>

